<?php
class OfficeForms {
	
	private static $errorMsg = array();
	
	public static function slaveMarketForm(Profile $profile, $request) {
			
			$units = DBUtils::getUnits();
			echo "<form method=\"POST\" action=\"office.php?action=slave_market_active&unit_id=".$request['unit_id']."\"><table id=\"units\">";
			foreach($units as $unit) {
					
				if($unit['unit_id'] == $request['unit_id']) {
					echo "<p>".$unit['description']."</p>";
				}
			}
			echo "<tr><td>Специалност</td><td>Атака</td><td>Защита</td><td>Цена</td><td>Брой</td></tr>";
			
			foreach($units as $unit) {
			
				if($unit['unit_id'] == $request['unit_id']) {
	
					echo "<tr>";
					echo "<td>".$unit['name']."</td>";
					echo "<td>".$unit['attack']."</td>";
					echo "<td>".$unit['deffence']."</td>";
					echo "<td>".$unit['price']."</td>";
					
					if(isset($request['buyUnit'])) {
						
						if (isset($request['unitAmount'])) {
							
							if($request['unitAmount']<= 0) {
								
								self::$errorMsg['unitAmount'] = "Моля посочи колко програмисти желаеш да наемеш!";
							}
						}
					}
					
					echo "<td><input type=\"number\" id=\"unitAmount\" name=\"unitAmount\"/></td>";
					echo "<td><button name=\"buyUnit\" id=\"buyUnit\"/>Наеми</button></td>";
					echo "</tr>";
					
					if(isset(self::$errorMsg['unitAmount'])) {
						echo "<tr><td colspan=\"5\" class=\"error\">".self::$errorMsg['unitAmount']."</td></tr>";
					} else {
						
						if(isset($request['buyUnit'])) {
							
							Utils::buyUnit($profile->getUser(), $request['unit_id'], $request['unitAmount']);
						}
					}
				}
			}
			
			echo "</form></table>";

	}
	
	public static function compBattleForm(Profile $profile, $request) {
		
		if(isset($request['hack'])) {
			
			Battle::computerBattle($profile->getUser());
			
		} else {
			
			echo "<form method=\"POST\" action=\"office.php?action=comp_battle_active\">";
			if ($profile->getUser()->getCompBattles() > 0 && $profile->getUser()->getCompBattles() <= 3)
				echo "<button name=\"hack\" id=\"hack\">Хакни системата!</button>";
			echo "</form>";
			
		}
	}
	
	public static function emptyPocketsForm(Profile $profile, $request) {
		
		$user = $profile->getUser();
		if(isset($request["right_answer"])) {
				
			echo "<p>Правилен отговор! Взимай 10 кинта и бягай!</p>";
			$user->setQuestionsReset(1);
			$currMoney = $user->getMoney();
			$user->setMoney($currMoney + 10);
				
		} else if(isset($request["wrong_answer_1"]) || 
		   isset($request["wrong_answer_2"]) || 
		   isset($request["wrong_answer_3"])) {
		
			echo "<p>Съжаляваме, но отговорът ти е грешен!</p>";
			echo "<p>Опитай пак утре !</p>";
			
			$user->setQuestionsReset(1);
		
		} else {
			
			$question = DBUtils::getQuestions();
			//var_dump($questions);
			echo "<p>".$question['question']."</p>";
			
			Utils::shuffle_assoc($question);
			
			echo "<form method=\"POST\" action=\"office.php?action=empty_pockets_active&question\">";
			foreach ($question as $key=>$value) {
			
				if(is_int($key) || $key == "question") {
			
					continue;
				}
				echo '<p><button id="'.$key.'" name="'.$key.'" class="button">'.$value.'</button></p>';
			}
			
			echo "</form>";
		}
		
	}
	
}